package com.example.user.newsongpaly;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;

import com.blankj.utilcode.util.FragmentUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends FragmentActivity {

    @BindView(R.id.FrameLayout)
    android.widget.FrameLayout FrameLayout;
    @BindView(R.id.SeekBar)
    android.widget.SeekBar SeekBar;
    @BindView(R.id.Previous)
    ImageView Previous;
    @BindView(R.id.start)
    ImageView start;
    @BindView(R.id.Next)
    ImageView Next;
    @BindView(R.id.Button1)
    Button Button1;
    @BindView(R.id.Button2)
    Button Button2;
    @BindView(R.id.Button3)
    Button Button3;
    Fragment fragment = FragmentList.newInstance();
    MyService.MyBinder myBinder;
    MyServiceConnection serviceConnection;
    private int i=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        showFragement(FragmentList.TAG,fragment);
        Intent intent = new Intent(this,MyService.class);
        serviceConnection = new MyServiceConnection();
        bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
    }
    public void showFragement(String tag, Fragment fragment) {
        FragmentUtils.addFragment(getSupportFragmentManager(), fragment, R.id.FrameLayout, false, true);
    }
    @OnClick({R.id.Previous, R.id.start, R.id.Next, R.id.Button1, R.id.Button2, R.id.Button3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.Previous:
                ListContentlist.getlistContentlist().setJianitem();
                if(ListContentlist.getlistContentlist().getitem()!=-2){
                    GengXin();
                }else {
                    Toast.makeText(this, "已经是第一首", Toast.LENGTH_SHORT).show();
                }
                start.setImageResource(R.drawable.star);
                i=1;
                break;
            case R.id.start:
                    Intent intente = new Intent(this,MyService.class);
                    intente.putExtra("kaishi",1);
                    startService(intente);
                    if(i == 0){
                        //设置开始图片
                        start.setImageResource(R.drawable.star);
                        i = 1;
                    }else {
                        //设置停止图片
                        start.setImageResource(R.drawable.stop);
                        i = 0;
                    }
                break;
            case R.id.Next:
                ListContentlist.getlistContentlist().setJiaitem();
                if(ListContentlist.getlistContentlist().getitem()!=-1){
                    GengXin();
                }else {
                    Toast.makeText(this, "已经是最后一首", Toast.LENGTH_SHORT).show();
                }
                start.setImageResource(R.drawable.star);
                i=1;
                break;
            case R.id.Button1:
                FragmentUtils.replaceFragment(fragment,FragmentList.newInstance());
                Button1.setBackgroundColor(Color.parseColor("#53acec"));
                Button2.setBackgroundColor(Color.parseColor("#d5d6d6"));
                Button3.setBackgroundColor(Color.parseColor("#d5d6d6"));
                break;
            case R.id.Button2:
                FragmentUtils.replaceFragment(fragment,FragmentSingerMessage.newInstance());
                Button2.setBackgroundColor(Color.parseColor("#53acec"));
                Button1.setBackgroundColor(Color.parseColor("#d5d6d6"));
                Button3.setBackgroundColor(Color.parseColor("#d5d6d6"));
                break;
            case R.id.Button3:
                FragmentUtils.replaceFragment(fragment,FragmentMessage.newInstance());
                Button3.setBackgroundColor(Color.parseColor("#53acec"));
                Button2.setBackgroundColor(Color.parseColor("#d5d6d6"));
                Button1.setBackgroundColor(Color.parseColor("#d5d6d6"));
                break;
        }
    }
    private void GengXin(){
        SongJsong.ShowapiResBodyBean.PagebeanBean.ContentlistBean contentlistBean = ListContentlist.getlistContentlist().getContentlistBean();
        Intent intent = new Intent(this,MyService.class);
        intent.putExtra("Url",contentlistBean.getM4a());
        startService(intent);
    }
    class MyServiceConnection implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            myBinder = (MyService.MyBinder) iBinder;
            myBinder.getmyService().setMyCellBack(new MyService.MyCellBack() {
                @Override
                public void CurrentPosition(int time, int longtime) {
                    Message message = new Message();
                    Bundle bundle = new Bundle();
                    bundle.putInt("time",time);
                    bundle.putInt("longtime",longtime);
                    message.setData(bundle);
                    handler.sendMessage(message);
                }
            });
        }
        Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.getData().getInt("longtime")==msg.getData().getInt("time")){
                    ListContentlist.getlistContentlist().setJiaitem();
                    GengXin();
                    return;
                }
                SeekBar.setMax(msg.getData().getInt("longtime"));
                SeekBar.setProgress(msg.getData().getInt("time"));
            }
        };
        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    }
}
